﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Data.SqlClient;

namespace 餐饮管理系统
{
	/// <summary>
	/// LoginWindow.xaml 的交互逻辑
	/// </summary>
	public partial class LoginWindow : Window
	{
        DatabaseConnect databaseConnect;
        MainWindow mainWindow;
		public LoginWindow(MainWindow mainWindow)
		{
			this.InitializeComponent();
			
			// 在此点之下插入创建对象所需的代码。
            databaseConnect = new DatabaseConnect();
            databaseConnect.Open();
            this.mainWindow = mainWindow;
		}

        private void OnClickCloseButton_Completed(object sender, EventArgs e)
        {
            databaseConnect.Close();
            this.Close();
        }

        private void OnClickLoginButton_Completed(object sender, EventArgs e)
        {
            databaseConnect.Close();
            //MainWindow mainWindow = new MainWindow();
            //MainWindow mainWindow = (this. as MainWindow);
            mainWindow.Show();
            this.Close();
        }

        private void MinStoryboard_Completed(object sender, EventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        private void MinButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	// 在此处添加事件处理程序实现。
			this.WindowState = WindowState.Minimized;
        }

        private void image_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
        	// TODO: Add event handler implementation here.
			this.DragMove();
        }

        private void LoginButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	SqlDataReader dataReader = databaseConnect.GetDataReader("select name,password from UserTable");
            while (dataReader.Read())
            {
                if (textBox.Text == dataReader[0].ToString().Trim()
                    && passwordBox.Password == dataReader[1].ToString().Trim())
                {
                    Storyboard loginStoryboard_True = (Storyboard)this.FindResource("OnClickLoginButton");
                    loginStoryboard_True.Begin();
                }
            }
            dataReader.Close();
            
            Storyboard loginStoryboard_False = (Storyboard)this.FindResource("OnClickLoginButton_False");
            loginStoryboard_False.Begin();
        }
	}
}